//
//  YFTeamedDetailVC.m
//  project
//
//  Created by Winny Zhang on 2019/3/26.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFTeamedDetailVC.h"
#import "YFILikeVC.h"
#import "YFLikeMeVC.h"
#import "HJLejianVC.h"
#import "TYTabPagerView.h"
#import "TYTabPagerController.h"
#import "YFJourneyModel.h"
#import "HJJourneyHeaderView.h"
#import "HJLineVC.h"

@interface YFTeamedDetailVC () <TYTabPagerBarDataSource ,TYTabPagerBarDelegate, TYPagerControllerDataSource, TYPagerControllerDelegate>

/**
 分页 Bar
 */
@property (nonatomic, strong, nonnull) TYTabPagerBar *pageBar;
/**
 分页控制器
 */
@property (nonatomic, strong, nonnull) TYPagerController *pageController;

/**
 Header View
 */
@property (nonatomic, strong, nonnull) HJJourneyHeaderView *headerView;

/**
 标题 数组
 */
@property (nonatomic, strong, nonnull) NSArray<NSString *> *titles;

/**
 View 数组
 */
@property (nonatomic, strong, nonnull) NSArray<UIViewController *> *viewControllers;

@end

@implementation YFTeamedDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"行程详情";
    [self setupViews];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self setupLayoutFrame];
}


#pragma mark - 设置 Sub Views
- (void)setupViews {
    [self setupHeader];
    [self setupLayout];
}

- (void)setupHeader {
    [self.view addSubview:self.headerView];
    self.headerView.journeyModel = self.journeyModel;
}

#pragma mark - 设置布局
- (void)setupLayout {
    [self setupPageBar];
    [self setupPageControlelr];
    [self loadLayout];
}

- (void)loadLayout {
    [self.pageBar reloadData];
    [self.pageController reloadData];
}
- (void)setupLayoutFrame {
    self.pageBar.frame = CGRectMake(10, JOURNEY_HEADER_HEIGHT, SCREEN_WIDTH-20, 45);
    CGFloat pageY = JOURNEY_HEADER_HEIGHT+45;
    CGFloat pageH = SCREEN_HEIGTH - pageY;
    self.pageController.view.frame = CGRectMake(0, pageY, SCREEN_WIDTH, pageH);
}

- (void)setupPageBar {
    self.titles = @[@"已成行", @"已乐见", @"喜欢我", @"我喜欢"];
    self.pageBar = [[TYTabPagerBar alloc] init];
    self.pageBar.delegate = self;
    self.pageBar.dataSource = self;
    self.pageBar.layout.cellEdging = 0;
    self.pageBar.layout.cellSpacing = 0;
    self.pageBar.layout.adjustContentCellsCenter = YES;
    self.pageBar.layout.barStyle = TYPagerBarStyleProgressElasticView;
    self.pageBar.layout.normalTextColor = [UIColor colorWithHex:0x999999];
    self.pageBar.layout.selectedTextColor = [UIColor colorWithHex:0x000000];
    self.pageBar.layout.progressColor = [UIColor colorWithHex:0xFF2400];
    self.pageBar.layout.normalTextFont = [UIFont boldSystemFontOfSize:15];
    self.pageBar.layout.selectedTextFont = [UIFont boldSystemFontOfSize:15];
    self.pageBar.layout.progressHeight = 4;
    self.pageBar.layout.cellWidth = (SCREEN_WIDTH - 20) / self.titles.count;
    self.pageBar.backgroundColor = [UIColor whiteColor];
    [self.pageBar rounded:UIRectCornerAllCorners radius:6];
    [self.pageBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
    [self.view addSubview:_pageBar];
}

- (void)setupPageControlelr {
    HJLineVC *vc1 = [HJLineVC new];
    vc1.tripId = _journeyModel.tripModel.journey_id;
    vc1.journeyModel = self.journeyModel;

    HJLejianVC *vc2 = [HJLejianVC new];
    vc2.tripId = _journeyModel.tripModel.journey_id;
    vc2.journeyModel = self.journeyModel;
    
    YFLikeMeVC *vc3 = [YFLikeMeVC new];
    vc3.tripId = _journeyModel.tripModel.journey_id;
    YFILikeVC *vc4 = [YFILikeVC new];
    vc4.tripId = _journeyModel.tripModel.journey_id;
    self.viewControllers = @[vc1,vc2, vc3, vc4];
    self.pageController = [[TYPagerController alloc] init];
    self.pageController.dataSource = self;
    self.pageController.delegate = self;
    self.pageController.layout.prefetchItemCount = 1;
    self.pageController.view.backgroundColor = self.view.backgroundColor;
    self.pageController.layout.addVisibleItemOnlyWhenScrollAnimatedEnd = YES;
    self.view.backgroundColor = [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    [self addChildViewController:_pageController];
    [self.view addSubview:_pageController.view];
}

#pragma mark - 布局的 DataSource Delegate
#pragma mark - TYTabPagerBarDataSource ,TYTabPagerBarDelegate
- (NSInteger)numberOfItemsInPagerTabBar {
    return _titles.count;
}

- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar
                                              cellForItemAtIndex:(NSInteger)index {
    UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
    cell.titleLabel.text = _titles[index];
    return cell;
}

- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    NSString *title = self.titles[index];
    return [pagerTabBar cellWidthForTitle:title];
}

- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    [self.pageController scrollToControllerAtIndex:index animate:YES];
}

#pragma mark - TYTabPagerControllerDataSource, TYTabPagerControllerDelegate
- (NSInteger)numberOfControllersInPagerController {
    return _viewControllers.count;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    return _viewControllers[index];
}

-(void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    [self.pageBar scrollToItemFromIndex:fromIndex toIndex:toIndex animate:animated];
}

- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    [self.pageBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
}


#pragma mark - Lazy Load
- (HJJourneyHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[HJJourneyHeaderView alloc]init];
        _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, JOURNEY_HEADER_HEIGHT);
    }
    return _headerView;
}

@end
